//
//  NBALoginViewController.m
//  NBAirCleaner
//
//  Created by 白浩洋 on 2018/2/23.
//  Copyright © 2018年 HaoYang. All rights reserved.
//

#import "NBALoginViewController.h"
#import "NBARegisterViewController.h"
#import "AppDelegate.h"
@interface NBALoginViewController ()
@property (nonatomic,strong)UITextField * phoneFiled;
@property (nonatomic,strong)UITextField * psdFiled;
@property (nonatomic,strong)UIButton * loginBtn;
@property (nonatomic,strong)UIButton * registerBtn;
@property (nonatomic,strong)UIButton * forgetBtn;
@end

@implementation NBALoginViewController
{
    JGProgressHUD *HUD;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpUI];
}
- (void)getState11{
    //查找GameScore表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"audit"];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error){
            //进行错误处理
        }else{
            //表里有id为0c6db13c的数据
            if (array.count >0) {
                //得到playerName和cheatMode
                NSDictionary * dic = array[0];
                BOOL cheatMode = [[dic objectForKey:@"isopen"]  isEqualToString:@"开放"];
                float versionss = [[dic objectForKey:@"version"] floatValue];
                NSString *aboutStr =[dic objectForKey:@"about"];
                [NBATools setAbout:aboutStr];
                [NBATools setSHState:cheatMode];
                [NBATools setVersions:versionss];
                
            }
        }
    }];
}

- (void)getState{
    //查找GameScore表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"audit"];
    //查找GameScore表里面id为0c6db13c的数据
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error){
            //进行错误处理
        }else{
            //表里有id为0c6db13c的数据
            if (array.count >0) {
                //得到playerName和cheatMode
                NSDictionary * dic = array[0];
                BOOL cheatMode =  [[dic objectForKey:@"isopen"]  isEqualToString:@"开放"];
                float versionss = [[dic objectForKey:@"version"] floatValue];
                NSString *abstr = [dic objectForKey:@"about"];
                [NBATools setAbout:abstr];
                [NBATools setSHState:cheatMode];
                [NBATools setVersions:versionss];
                [BmobUser loginWithUsernameInBackground:self.phoneFiled.text password:self.psdFiled.text block:^(BmobUser *user, NSError *error) {
                    if(!error){
                        [HUD dismissAfterDelay:0.0];
                        NSUserDefaults *nsud = [NSUserDefaults standardUserDefaults];
                        [nsud setObject:self.phoneFiled.text forKey:@"username"];
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"loginSuccess" object:nil];
                        BmobUser *user = [BmobUser currentUser];
                        //查找GameScore表
                        BmobQuery   *bquery = [BmobQuery queryWithClassName:@"star"];
                        //查找GameScore表里面id为0c6db13c的数据
                        [bquery whereKey:@"userid" equalTo:user.username];
                        [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                            for (BmobObject *obj in array) {
                                NSString *sum = [obj objectForKey:@"star"];
                                [NBATools setstar:[sum integerValue]];
                                break;
                            }
                        }];
                    }else{
                        NSLog(@"登录失败 -- 》%@",error);
                        [HUD dismissAfterDelay:0.0];
                        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
                        [SVProgressHUD showInfoWithStatus:@"登录失败,用户名或密码错误"];
                        [SVProgressHUD dismissWithDelay:0.6];
                    }
                }];
            }
        }
    }];
}
- (void)setUpUI{
    UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    bgimage.image = [UIImage imageNamed:@"login_bg"];
    [self.view addSubview:bgimage];
    UIImageView *logoimage = [[UIImageView alloc]initWithFrame:CGRectMake(PXChange(281), PXChange(162) - 64, PXChange(120), PXChange(120))];
    logoimage.image = [UIImage imageNamed:@"pan"];
    logoimage.center = CGPointMake(ScreenWidth/2.0f, PXChange(200) + logoimage.height/2.0f);
    [self.view addSubview:logoimage];
    self.phoneFiled.center = CGPointMake(ScreenWidth/2.0f, logoimage.bottom + PXChange(80) +self.phoneFiled.height/2.0f);
    UILabel *lineOne = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), 1)];
    lineOne.backgroundColor = [UIColor colorWithHexString:@"#EDEDF0"];
    lineOne.center = CGPointMake(ScreenWidth/2.0f, self.phoneFiled.bottom);
    [self.view addSubview:lineOne];
    self.psdFiled.center = CGPointMake(ScreenWidth/2.0f, self.phoneFiled.bottom + PXChange(36) +self.phoneFiled.height/2.0f);
    UILabel *lineTwo = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), 1)];
    lineTwo.backgroundColor = [UIColor colorWithHexString:@"#EDEDF0"];
    lineTwo.center = CGPointMake(ScreenWidth/2.0f, self.psdFiled.bottom);
    [self.view addSubview:lineTwo];
    self.loginBtn.center = CGPointMake(ScreenWidth/2.0f, PXChange(60) + lineTwo.bottom + self.loginBtn.height/2.0f);
    self.registerBtn.center = CGPointMake(ScreenWidth/2.0f, PXChange(27) + self.loginBtn.bottom + self.registerBtn.height/2.0f);
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, PXChange(330), PXChange(38))];
    image.image = [UIImage imageNamed:@"love"];
    image.center = CGPointMake(ScreenWidth/2.0f, ScreenHeight - PXChange(48) - image.height/2.0f);
    [self.view addSubview:image];
    
    UILabel *desLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(200), 0)];
    desLabel.backgroundColor = [UIColor clearColor];
    desLabel.numberOfLines = 0;
    desLabel.text = @"欢迎使用本软件\n激情影视 值得拥有";
    desLabel.textAlignment = NSTextAlignmentCenter;
    desLabel.font = [UIFont systemFontOfSize:PXChange(20)];
    desLabel.textColor = [UIColor whiteColor];
    [desLabel sizeToFit];
    desLabel.center = CGPointMake(ScreenWidth/2.0f, ScreenHeight - desLabel.height/2.0f - PXChange(25));
    [self.view addSubview:desLabel];

}
- (void)loginClick{
    if(![NBATools isMobileNumber:self.phoneFiled.text]){
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
        [SVProgressHUD showInfoWithStatus:@"请输入正确的手机号码"];
        [SVProgressHUD dismissWithDelay:0.6];
        return;
    }
    if(self.psdFiled.text.length < 6){
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
        [SVProgressHUD showInfoWithStatus:@"请检查密码"];
        [SVProgressHUD dismissWithDelay:0.6];
        return;
    }
    [self getState];
}
- (void)registerClick{
    NBARegisterViewController *nbr = [[NBARegisterViewController alloc]init];
    nbr.type = 1;
    [self.navigationController pushViewController:nbr animated:YES];
}
- (void)forgetClick{
    NBARegisterViewController *nbr = [[NBARegisterViewController alloc]init];
    nbr.type = 2;
    [self.navigationController pushViewController:nbr animated:YES];
}
-(void)textFieldDidChange :(UITextField *)theTextField{
    if(self.phoneFiled.text.length >0 && self.psdFiled.text.length >0){
        [self.loginBtn setEnabled:YES];
        [_loginBtn setTitleColor:[UIColor colorWithHexString:BH_Color_Main] forState:UIControlStateNormal];

    }else{
        [self.loginBtn setEnabled:NO];
        [_loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

    }
}
- (UITextField *)phoneFiled{
    if(!_phoneFiled){
        _phoneFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), PXChange(104))];
        NSString *holderText = @"请输入手机号";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#C7C7CD"] range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:PXChange(36)] range:NSMakeRange(0, holderText.length)];
        _phoneFiled.attributedPlaceholder = placeholder;
        _phoneFiled.borderStyle = UITextBorderStyleNone;
        _phoneFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        _phoneFiled.keyboardType = UIKeyboardTypeDefault;
        _phoneFiled.textColor = [UIColor whiteColor];
        [_phoneFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self.view addSubview:_phoneFiled];
    }
    return _phoneFiled;
}
- (UITextField *)psdFiled{
    if(!_psdFiled){
        _psdFiled = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth - PXChange(144), PXChange(104))];
        NSString *holderText = @"请输入密码";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#C7C7CD"] range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:PXChange(36)] range:NSMakeRange(0, holderText.length)];
        _psdFiled.attributedPlaceholder = placeholder;
        _psdFiled.borderStyle = UITextBorderStyleNone;
        _psdFiled.keyboardType = UIKeyboardTypeASCIICapable;
        _psdFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        _psdFiled.secureTextEntry = YES;
        _psdFiled.textColor = [UIColor whiteColor];
        [_psdFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self.view addSubview:_psdFiled];
    }
    return _psdFiled;
}
- (UIButton *)loginBtn{
    if(!_loginBtn){
        _loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PXChange(600), PXChange(100))];
        [_loginBtn setBackgroundColor:[UIColor whiteColor]];
        [_loginBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_loginBtn.titleLabel setFont:[UIFont systemFontOfSize:PXChange(36)]];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.layer.cornerRadius = PXChange(50);
        _loginBtn.clipsToBounds = YES;
        [_loginBtn addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
        [_loginBtn setEnabled:NO];
        [self.view addSubview:_loginBtn];
    }
    return _loginBtn;
}
- (UIButton *)registerBtn{
    if(!_registerBtn){
        _registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PXChange(146), PXChange(40))];
        [_registerBtn setBackgroundColor:[UIColor clearColor]];
        [_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_registerBtn.titleLabel setFont:[UIFont systemFontOfSize:PXChange(28)]];
        [_registerBtn setTitle:@"新用户注册" forState:UIControlStateNormal];
        [_registerBtn addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_registerBtn];
    }
    return _registerBtn;
}
- (UIButton *)forgetBtn{
    if(!_forgetBtn){
        _forgetBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, PXChange(118), PXChange(40))];
        [_forgetBtn setBackgroundColor:[UIColor clearColor]];
        [_forgetBtn setTitleColor:[UIColor colorWithHexString:@"#5B729A"] forState:UIControlStateNormal];
        [_forgetBtn.titleLabel setFont:[UIFont systemFontOfSize:PXChange(28)]];
        [_forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
        [_forgetBtn addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_forgetBtn];
    }
    return _forgetBtn;
}
@end
